import time

import websitebase


class okcoin(websitebase.websitebase):
    _url_ticker = "https://www.okcoin.cn/api/v1/ticker.do?symbol=%s"

    def __init__(self):
        websitebase.websitebase.__init__(self)

    def getlast(self, symbol="btc_cny"):
        if self._string_is_empty(symbol):
            print("okcoin.getlast:输入的参数不能为空")
            return
        jdata = self.ticker(symbol)
        return jdata['ticker']['last']

    # symbol:"btc_cny"/"ltc_cny"/"eth_cny"
    def ticker(self, symbol="btc_cny"):
        if self._string_is_empty(symbol):
            print("okcoin.ticker:输入的参数不能为空")
            return
        if symbol != "btc_cny" and symbol != "ltc_cny" and symbol != "eth_cny":
            print("okcoin.ticker:输入的参数不正确")
            return
        url = self._url_ticker % (str(symbol))
        return self._req_json(url)


if __name__ == "__main__":
    t = time.time()
    a = okcoin()
    print(a.getlast())
    # print(a.getlast(""))
    print(time.time() - t)



# url = "https://www.okcoin.cn/api/v1/ticker.do?symbol=btc_cny"
#
# re = urllib.request.urlopen(url)
# data = re.read().decode("utf-8")
# print(data)
# jdata = json.loads(data)
# print(jdata)
# print(jdata['ticker']['last'])
